package com.smi.cn.satemachine.annotation;

import com.smi.cn.satemachine.ReturnVo;
import com.smi.cn.satemachine.StateMachineContext;
import com.smi.cn.satemachine.StatemachineUtil;
import org.springframework.stereotype.Service;
import org.squirrelframework.foundation.exception.TransitionException;
import org.squirrelframework.foundation.fsm.UntypedStateMachine;
import org.squirrelframework.foundation.fsm.annotation.*;

@Service
public class PersistModule {


    @OnTransitionBegin
    public void transitionBegin(ReturnVo.RmaStatus from, ReturnVo.RmaTrigger event, StateMachineContext context) {
        System.out.println("transitionBegin");
    }


    @OnTransitionComplete
    public void transitionComplete(ReturnVo.RmaStatus from, ReturnVo.RmaStatus to, ReturnVo.RmaTrigger event, StateMachineContext context) {
        System.out.println("OnTransitionComplete");

    }

    @OnTransitionException
    public void transitionCauseException(ReturnVo.RmaStatus from, ReturnVo.RmaStatus to, ReturnVo.RmaTrigger event,
                                         StateMachineContext context, TransitionException e){
        System.out.println("transitionCauseException");
        e.printStackTrace();
    }

    @OnTransitionDecline
    public void transitionDeclined(ReturnVo.RmaStatus from, ReturnVo.RmaTrigger event, Integer context) {
        System.out.println("transitionDeclined 无效路径！ error");
    }

    @OnTransitionEnd
    @ListenerOrder(10) // Since 0.3.1 ListenerOrder can be used to insure listener invoked orderly
    public void transitionEnd(ReturnVo.RmaStatus from, ReturnVo.RmaStatus to, ReturnVo.RmaTrigger rmaTrigger, StateMachineContext stateMachineContext,
                              UntypedStateMachine stateMachine) {
        System.out.println("OnTransitionEnd");
        System.out.println(StatemachineUtil.getTransitDesc(to, rmaTrigger, stateMachine));
    }

    @OnBeforeActionExecuted
    public void onBeforeActionExecuted(ReturnVo.RmaStatus sourceState, ReturnVo.RmaStatus targetState,
                                       ReturnVo.RmaTrigger event, StateMachineContext context) {
        System.out.println("onBeforeActionExecuted");
    }

    @OnAfterActionExecuted
    public void onAfterActionExecuted(ReturnVo.RmaStatus sourceState, ReturnVo.RmaStatus targetState,
                                      ReturnVo.RmaTrigger event, StateMachineContext context) {
        System.out.println("onAfterActionExecuted");
    }

    @OnActionExecException
    public void onActionExecException(TransitionException e) {
        System.out.println("onActionExecException");
        e.printStackTrace();
    }

}
